'''
A script that runs the FTP server & FTP client applications under
TwistedSim using process based emulation.

@author: Hatem Oraby
'''
import twistedSim.simManager as simManager 
from twistedSim.moduleHandler import twistedH
from twistedSim.contextHandlers import ProcessHandler

def getTime():
    import time
    startTime = time.time()
    def printTime():
        import time
        from twistedSim.overrideManager import OverrideManager
        realAscTime = OverrideManager.getEntity("time.asctime")
        realTime = OverrideManager.getEntity("time.time")
        #startTime = time.time()
        print "Time now: " + time.asctime()
        print "Real TIme: " + realAscTime()
        print "Simulated - Real = " + str(time.time() - realTime())
        print "Real time since i started: " + str(realTime() - startTime)
        print "Time since i started: " + str(time.time() - startTime)
    simManager.SimManager.addPostRun(printTime)

if __name__ == '__main__':
    import os
    #Module 1
    path  = os.path.realpath(".") + "/Server"
    module1 = ProcessHandler('ftpserver.py', twistedH(), path, startTime = 50)
    machine1 = simManager.Machine()
    #interface1 = simManager.IpInterface('176.10.0.1',machine1)
    simManager.SimManager.addModule(module1,machine1)
    
    #Module 2-10
    path  = os.path.realpath(".") + "/Client"
    for i in range(1,10):
        filename = "receivedFile"+str(i)+".txt"
        module2 = ProcessHandler('ftpclient.py', twistedH(), path,
                                 startTime = 100 + i,
                                 sysArgs = "-f " + filename, 
                                 nickName = "FtpClient_"+str(i))
        #machine2 = simManager.Machine()
        #interface2 = simManager.IpInterface('42.30.125.96',machine2)
        simManager.SimManager.addModule(module2,machine1)
    simManager.SimManager.addPreRun(getTime)
    simManager.SimManager.run()